<?php

namespace App\Http\Controllers\applet_api;

use App\Http\Controllers\Controller;
use App\Models\Resources;
use App\Models\ResourcesType;
use Illuminate\Http\Request;

class ResourcesController extends Controller
{

    public function index(Request $Request)
    {
        $search = $Request->search;
        $type = $Request->type;
        $limit = $Request->limit ?: 10;
        $page = $Request->page ?: 1;
        $page = $page * $limit - $limit;
        $sql = Resources::select('id','title','log','addr')->with('types')->when($search, function ($query, $search) {
                    return $query->where('title','like', $search.'%');
                })->when($type, function ($query, $type) {
                    return $query->where('type',$type);
                });
        $total = $sql->count();
        $data = $sql->skip($page)->take($limit)->orderBy('id','desc')->get();
        $types = ResourcesType::get();
        return response()->json(['code' => '200', 'msg' => 'success','data'=>$data,'total'=>$total,'types'=>$types]);
    }

    public function info(Request $Request)
    {
        $data = Resources::find($Request->id);
        $data->content = str_replace('src="/ueditor/php/upload','src="https://gl.xinzhuvip.com/ueditor/php/upload',$data->content);
        return response()->json(['code' => '200', 'msg' => 'success','data'=>$data]);
    }
}
